package com.heo.util;

import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class GsonUtils {

	/**
	 * 
	 * @param obj
	 * @return
	 */
	public static String objectToJson(Object obj) {
		Gson gson = new Gson();
		String json = gson.toJson(obj);
		return json;
	}
	
	/**
	 * json数据转换为具体的object
	 * 
	 * @param jsonString
	 * @param obj
	 * @return
	 */
	public static Object jsonToObject(String jsonString,
			Class<? extends Object> clas) {
		Gson gson = new Gson();
		return gson.fromJson(jsonString, clas);
	}
	
	/**
	 * 
	 * @param jsonString
	 * @return
	 */
	public static Map<String, Object> jsonToMap(String jsonString) {
		Gson gson = new Gson();
		Map<String, Object> maps = gson.fromJson(jsonString,
				new TypeToken<Map<String, Object>>() {
				}.getType());
		return maps;
	}
}
